package com.ruitu.mall_98_2.mine.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beanu.arad.base.ToolBarFragment;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.TimeUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.CouponContract;
import com.ruitu.mall_98_2.mvp.model.CouponModel;
import com.ruitu.mall_98_2.mvp.presenter.CouponPresenter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.mine.Coupon;

import java.text.SimpleDateFormat;
import java.util.List;

public class FragmentInvalidCoupon extends ToolBarFragment<CouponPresenter, CouponModel> implements CouponContract.View {
    public static FragmentInvalidCoupon newInstance(int position) {

        Bundle args = new Bundle();
        args.putInt("position", position);
        FragmentInvalidCoupon fragment = new FragmentInvalidCoupon();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coupon, null);
        initView(view);
        return view;
    }

    int positions;
    private ProgressLayout progressLayout;
    private LRecyclerView lRecyclerView;

    private void initView(View view) {
        lRecyclerView = view.findViewById(R.id.lRecyclerView);
        progressLayout = view.findViewById(R.id.progress);
        progressLayout.showLoading();
        lRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new DataAdapter(getContext());
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lRecyclerView.setAdapter(lRecyclerViewAdapter);
        positions = getArguments().getInt("position") + 1;
        Logg.i("position: " + positions);
        getData();
        lRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        lRecyclerView.setLoadMoreEnabled(false);
    }

    private void getData() {
        mPresenter.getMineCouponsNew(AppHolder.instance().getUser().getAccountId(), positions + "");
    }

    @Override
    public void setData(List<Coupon> coupons) {
        lRecyclerView.refreshComplete(20);
        if (isDataEmpty(coupons)) {
            progressLayout.showEmpty(R.drawable.yewubaobiao, "暂无数据", "");
        } else {
            adapter.setDataList(coupons);
            progressLayout.showContent();
        }
    }

    @Override
    public void setFail() {
        lRecyclerView.refreshComplete(20);
        progressLayout.showError(R.mipmap.no_content, "加载异常", "", "重新加载", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgress();
                getData();
            }
        });
    }

    DataAdapter adapter;

    class DataAdapter extends ListBaseAdapter<Coupon> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_invalid_coupon;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_sum = holder.getView(R.id.tv_sum);
            TextView tv_user_type = holder.getView(R.id.tv_user_type);
            TextView tv_user_restrict = holder.getView(R.id.tv_user_restrict);
            TextView tv_useful_life = holder.getView(R.id.tv_useful_life);
            TextView tv_coupon_status = holder.getView(R.id.tv_coupon_status);

            if (positions == 2) {
                tv_coupon_status.setText("已使用");
            } else if (positions == 3) {
                tv_coupon_status.setText("已失效");
            }
            final Coupon coupon = mDataList.get(position);
            if (TextUtils.equals(coupon.getStatus(), "1")) {
                tv_sum.setText(coupon.getDiscountRatio() + "%");
                tv_user_restrict.setText("企业内商品优惠" + coupon.getDiscountRatio() + "%");
            } else {
                tv_sum.setText("￥" + coupon.getReduceMoney());
                tv_user_restrict.setText("订单金额满" + coupon.getSatisfyMoney() + "元可使用");
            }

            if (TextUtils.equals(coupon.getShopType(), "0")) {
                tv_user_type.setText(coupon.getCompanyName() + "可用");
            } else {
                tv_user_type.setText("全类商品通用");
            }
            tv_useful_life.setText("有效期:" + TimeUtils.millis2String(Long.parseLong(coupon.getCreateTime()), new SimpleDateFormat("yyyy-MM-dd")) + " " + TimeUtils.millis2String(Long.parseLong(coupon.getUseTime()), new SimpleDateFormat("yyyy-MM-dd")));
        }

    }
}
